﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wechat.Results
{
    /// <summary>
    /// 图文回复
    /// </summary>
    public class WechatArticleResult : WechatResult
    {
        public WechatArticleResult(string toUsername, string fromUsername)
            : base(toUsername, fromUsername)
        {
            Items = new WechatArticle[0];
        }

        /// <summary>
        /// 文章数量
        /// </summary>
        public int ArticleCount
        {
            get { return Items.Length; }
        }

        /// <summary>
        /// 详细项
        /// </summary>
        public WechatArticle[] Items { get; set; }

        protected override void Format(StringBuilder builder)
        {
            builder.Append("<MsgType><![CDATA[news]]></MsgType>");

            builder.AppendFormat("<ArticleCount>{0}</ArticleCount>", ArticleCount);

            builder.Append("<Articles>");

            for (int i = 0; i < Items.Length; i++)
            {
                var item = Items[i];
                builder.AppendFormat("<item>");
                if (!string.IsNullOrEmpty(item.Url)) { builder.AppendFormat("<Url><![CDATA[{0}]]></Url>", item.Url); }
                if (!string.IsNullOrEmpty(item.Title)) { builder.AppendFormat("<Title><![CDATA[{0}]]></Title>", item.Title); }
                if (!string.IsNullOrEmpty(item.PicUrl)) { builder.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl>", item.PicUrl); }
                if (!string.IsNullOrEmpty(item.Description)) { builder.AppendFormat("<Description><![CDATA[{0}]]></Description>", item.Description); }
                builder.AppendFormat("</item>");
            }

            builder.Append("</Articles>");
        }
    }
    
    public class WechatArticle
    {
        /// <summary>
        /// 点击后跳转的链接
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 图片
        /// </summary>
        public string PicUrl { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
    }
}
